<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文件名称" prop="fileName">
        <el-input v-model="queryParams.fileName" placeholder="请输入文件名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['file:record:add']">新增文件</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键编号" align="center" prop="id"/>
      <el-table-column label="文件名称" align="center" prop="fileName"/>
      <el-table-column label="源文件地址" align="center" prop="sourceFileUrl"/>
      <el-table-column label="转换文件地址1版" align="center" prop="convertFile1"/>
      <el-table-column label="转换文件地址2版" align="center" prop="convertFile2"/>
      <el-table-column label="热点区域坐标" align="center" prop="areaCoords"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:record:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:record:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleViewImage(scope.row)">查看文件</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleFileEdit(scope.row)">编辑文件</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改文件记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <FileUpload :limit=1 v-model="form.sourceFileUrl" :fileType="['dwg']"></FileUpload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看文件对话框 -->
    <el-dialog :title="fileRecord.fileName" :visible.sync="viewFileRecordOpen" width="80%" append-to-body>
      <img usemap="#commodities" :src="fileRecord.convertFile1"/>
      <map name="commodities">
        <span v-for="area in areaList">
        <area :shape="area.rect" :coords="area.coords" :href="area.href" :target="area.target" :alt="area.alt"/>
      </span>
      </map>
    </el-dialog>
  </div>
</template>

<script>
import { addRecord, delRecord, getRecord, listRecord, updateRecord } from '@/api/test/fileRecord'
import FileUpload from '@/components/FileUpload'

export default {
  name: 'FileRecord',
  components: { FileUpload },
  data() {
    return {
      loading: true,                                                      // 遮罩层
      ids: [],                                                            // 选中数组
      single: true,                                                       // 非单个禁用
      multiple: true,                                                     // 非多个禁用
      showSearch: true,                                                   // 显示搜索条件
      total: 0,                                                           // 总条数
      recordList: [],                                                     // 文件记录表格数据
      title: '',                                                          // 弹出层标题
      open: false,                                                        // 是否显示弹出层
      queryParams: {                                                      // 查询参数
        pageNum: 1,
        pageSize: 10,
        fileName: null,
        sourceFileUrl: null,
        convertFile1: null,
        convertFile2: null,
        areaCoords: null
      },
      form: {                                                             // 表单参数
      },
      rules: {                                                            // 表单校验
      },
      viewFileRecordOpen: false,
      fileRecord: {},
      areaList: [
        { shape: 'rect', coords: '0,0,128,106', href: 'https://ligang.blog.csdn.net/?name=mango', target: '_blank', alt: '芒果' },
        { shape: 'rect', coords: '128,0,256,106', href: 'https://ligang.blog.csdn.net/?name=celery', target: '_blank', alt: '芹菜' },
        { shape: 'rect', coords: '256,0,386,106', href: 'https://ligang.blog.csdn.net/?name=lemon', target: '_blank', alt: '柠檬' },
        { shape: 'rect', coords: '0,106,128,212', href: 'https://ligang.blog.csdn.net/?name=sweetpotato', target: '_blank', alt: '红薯' },
        { shape: 'rect', coords: '128,106,256,212', href: 'https://ligang.blog.csdn.net/?name=cherry', target: '_blank', alt: '车厘子' },
        { shape: 'rect', coords: '256,106,386,212', href: 'https://ligang.blog.csdn.net/?name=corn', target: '_blank', alt: '玉米' },
        { shape: 'rect', coords: '0,212,128,318', href: 'https://ligang.blog.csdn.net/?name=crab', target: '_blank', alt: '螃蟹' },
        { shape: 'rect', coords: '128,212,256,318', href: 'https://ligang.blog.csdn.net/?name=pork', target: '_blank', alt: '猪肉' },
        { shape: 'rect', coords: '256,212,256,318', href: 'https://ligang.blog.csdn.net/?name=pickledcabbage', target: '_blank', alt: '酸菜' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询文件记录列表 */
    getList() {
      this.loading = true
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fileName: null,
        sourceFileUrl: null,
        convertFile1: null,
        convertFile2: null,
        areaCoords: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加文件记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改文件记录'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除文件记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 查看文件操作 */
    handleViewImage: function(row) {
      this.fileRecord = row
      this.viewFileRecordOpen = true
    },
    /** 编辑文件操作 */
    handleFileEdit: function(row) {
      this.$store.commit('FILE_RECORD', row)
      this.$router.push({ path: '/file/fileEdit?fileId=' + row.id })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
